var member_id = ws = '';
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	member_id = plus.storage.getItem('id');
//	console.log(member_id);
	$.ajax({ 
		type:"post",
		url:apiRoot + "?act=member_data&op=grade",
		data:{member_id:member_id},
		dataType:'json', 
		success:function(data){
			console.log(JSON.stringify(data));
			if(data){ 
				$('.member_card').attr('src',data.member_card?(app_pic + member_id + '/' + data.member_card):'../public/img/pic_img.jpg');
				$('.member_num').val(data.member_num);
				$('.member_school').val(data.member_school);
				$('.member_system').val(data.member_system);
				$('.member_grade').val(data.member_grade);
				$('.member_class').val(data.member_class);
			}
		},
		error:function(e){
			
		}
	});
	
	//上传学生证
	$('.member_card').on('click',function(){
		plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]},function(e){
			if(e.index == 1){
				var car =plus.camera.getCamera();
				car.captureImage(function(path){
					//展示图片
//					alert(plus.io.convertLocalFileSystemURL(path));
//					$('.shangchuan').attr('src','file://' + plus.io.convertLocalFileSystemURL(path));
					appendpic(path);
					
				},function(err){});
				
			}else if(e.index == 2){
				plus.gallery.pick(function(path){
//					$('.shangchuan').attr('src',path);
					appendpic(path);
				});
			}
		});
	})
	
	
	
	$('.preservation').on('click',function(){
		var str = $('.member_card').attr('src');
		var index = str.lastIndexOf("\/");  
		var member_card = str.substring(index + 1, str.length);
		var member_num = $('.member_num').val();
		var member_school = $('.member_school').val();
		var member_system = $('.member_system').val();
		var member_grade = $('.member_grade').val();
		var member_class = $('.member_class').val();
		
		
		
		$.ajax({
			type:"post",
			url:apiRoot + "?act=member_data&op=gradeiplode",
			data:{
				member_id:member_id,
				member_card:member_card,
				member_num:member_num,
				member_school:member_school,
				member_system:member_system,
				member_grade:member_grade,
				member_class:member_class
			},
			dataType:'json',
			success:function(data){
//				console.log(JSON.stringify(data));
				if(data){
					toast(data.msg);
					setTimeout(function(){
						ws.close();
					},1500)
				}
			},
			error:function(e){
				console.log(JSON.stringify(e));
				toast('连接失败！');
			}
			
		});
		
		
	})
	
},false)
//添加图片
var index = 1;
var files = [];
function appendpic(p){
	plus.nativeUI.showWaiting('图片处理中...');
//		console.log(p); 
	//创建新的路径
//				var newpath = p.replace(/\./g , new Date().gettime() + '.');
	var newpath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
			src : p,
			dst : newpath,
			quality : 20
		},
		function(){//毁掉成功
			files.push({name : "uploadkey" + index,path:p});
			index++;
			upload();
		},
		function(error){
		alert('压缩图片失败');
		plus.nativeUI.closeWaiting();
	})
}

function upload(){
	if(files.length <= 0){
		toast("请上传头像！");
	}
//				console.log(JSON.stringify(files.path));return;
	var server = apiRoot + "?act=sns_album&op=file_uploadone&member_id="+member_id;  
	var task = plus.uploader.createUpload(server,
	    {method:"post"},
		function(t,status){ //上传完成
			if(status == 200){ 
				plus.nativeUI.closeWaiting();
//					console.log(123);
				var infopics = $.parseJSON(t.responseText);
					console.log(JSON.stringify(infopics));
				if(infopics.code == 200){
					$('.member_card').attr('src',app_pic + member_id + '/' + infopics.datas.file_name);
				}else{
					toast(infopics.datas.error);
				}
//					plus.nativeUI.toast('处理完成' + t.responseText);
			}else{
				toast('上传失败：' + status);
				plus.nativeUI.closeWaiting();
			}
			
		},function () {
			plus.nativeUI.closeWaiting();
		}
		
	); 
	for(var i = 0;i　<　files.length ; i++){
		var f =files[i];
		task.addFile(f.path,{key:'file'});
//					    console.log(f.name)
	}
	task.start();
	 
}	